//
//  GuideViewController.swift
//  FoundationsAppCode
//
//  Created by Erickson on 16/3/18.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit



class GuideViewController: UIViewController ,UIScrollViewDelegate{

    var pageControl = HDRewritePageControl()
    
    var scrollView: UIScrollView!
    let colorArray = [0x5e8bd9,0x537ec7,0x4b5bad,0x313662]
    let imageArray = ["引导页1","引导页2","引导页3","引导页4"]
    let imageArray4s = ["引导页1－4s","引导页2－4s","引导页3－4s","引导页4－4s"]
    let numOfPages = 5
    let frame = UIScreen.main.bounds
    var lastView:HDGuideLastView?
    let deviceName = UIDevice.current.name  //iPhone Simulator   iPhone 4s
    
    override func viewWillAppear(_ animated: Bool) {
        navigationController?.navigationBar.isHidden = true
    }
    override func viewDidDisappear(_ animated: Bool) {
        navigationController?.navigationBar.isHidden = false
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setupScrollView()
        setupView1()
        lastView?.lookDelegate = self
        initPageImage(true, pageName: "未选中的状态", currentPageName: "选中的状态")
        
    }
    
    func initPageImage(_ showPage:Bool, pageName:String?, currentPageName:String?) {
        pageControl.isHidden = false
        if !showPage{
            pageControl.isHidden = true
        }
        if (pageName != nil) {
            pageControl.pageImage = pageName;
        }
        if (currentPageName != nil) {
            pageControl.currentPageImage = currentPageName;
        }
        
    }
    
    
    func setupScrollView(){
        scrollView = UIScrollView(frame: frame)
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.bounces = false
        scrollView.contentOffset = CGPoint.zero
        scrollView.contentSize = CGSize(width: frame.size.width * CGFloat(numOfPages), height: 0)
        scrollView.delegate = self
        scrollView.backgroundColor = UIColor.white
        self.view.addSubview(scrollView)
        
        pageControl.currentPageIndicatorTintColor = UIColor.black
        pageControl.pageIndicatorTintColor = UIColor.gray
        pageControl.numberOfPages = 5
        
        self.view.addSubview(pageControl)
        pageControl.snp.makeConstraints { (make) in
            make.left.equalTo(100)
            make.right.equalTo(-100)
            make.height.equalTo(30)
            make.bottom.equalTo(-38)
        }
    }
    
    //前四页内容
    func setupView1(){
        
            for index in 0..<4 {
                let topView = UIView()
                topView.frame = CGRect(x: frame.size.width * CGFloat(index), y: 0, width: AppConstant.screenWidth, height: AppConstant.screenHeight)
                topView.backgroundColor = UIColor(hex: colorArray[index])
                
                let guideImage = UIImageView()
                if deviceName == "iPhone 4s" {
                    guideImage.image = UIImage(named: imageArray4s[index])
                }else{
                    guideImage.image = UIImage(named: imageArray[index])
                }

                topView.addSubview(guideImage)
                guideImage.snp.makeConstraints({ (make) in
                    make.left.equalTo(topView.snp.left).offset(0)
                    make.right.equalTo(topView.snp.right).offset(0)
                    make.top.equalTo(0)
                    make.bottom.equalTo(0)
                })
                
                scrollView.addSubview(topView)
            
        //第五页内容
                
                    let view2 = UIView()
                    view2.frame = CGRect(x: frame.size.width * 4, y: 0, width: frame.size.width, height: frame.size.height)
                    scrollView.addSubview(view2)
                    lastView = HDGuideLastView(frame: CGRect(x: 0,y: 0,width: AppConstant.screenWidth,height: AppConstant.screenHeight))
                    view2.addSubview(lastView!)
        }
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offset = scrollView.contentOffset
        // 随着滑动改变pageControl的状态
        pageControl.currentPage = Int(offset.x / view.bounds.width)
        if pageControl.currentPage == 4 {
            pageControl.isHidden = true
            scrollView.isScrollEnabled = false
        }else {
            pageControl.isHidden = false
        }
    }
    
    
}

extension GuideViewController: lookSomethingDelegate {
    
    func lookSomething() {
        let homeVC = ViewController()
        UIApplication.shared.keyWindow?.rootViewController = homeVC
    }
    
    func register() {
        let register = HDIdentitySelectVC()
        register.ToWhere = 1
        register.isHaveBackBtn = 1
        let nav = BaseNavigationController(rootViewController: register)
//        nav.navigationBar.isTranslucent=false;
        register.navigationController?.navigationBar.isTranslucent = false

//        self.navigationController?.pushViewController(register, animated: true)
        self .present(nav, animated: true, completion: nil);
        UserDefaults.standard.set(true, forKey: "GuideRegister")
        
    }
    
    func login() {
        let login = HDIdentitySelectVC()
        login.ToWhere = 0
        login.isHaveBackBtn = 1
        let Nav = BaseNavigationController(rootViewController: login)
        login.navigationController?.navigationBar.isTranslucent = false
        self .present(Nav, animated: true, completion: nil);
//        self.navigationController?.pushViewController(login, animated: true)
        UserDefaults.standard.set(true, forKey: "Guide")
        
    }
}
